import java.security.Security;
import java.util.Properties;
import java.util.Scanner;
 
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.Store;
import javax.mail.Folder;
import com.sun.mail.smtp.SMTPTransport;
import javax.mail.URLName;
import javax.mail.Address;
import javax.mail.Authenticator;

public class MailService
{
    private static String user = "admintracker.njrotc.troyhigh@gmail.com";
    private static String pass = ""; //forget it, fool >:]

    public static Message[] getMessages()
    {
        System.out.println("Opening connection to imap.gmail.com...");
        
        Properties props = new Properties();
        
        props.put("mail.imap.host", "imap.gmail.com");
        props.put("mail.imap.user", "username");
        props.put("mail.imap.port", "993");
        props.put("mail.imap.starttls.enable","true");
        props.put("mail.imap.auth", "true");
        
        props.put("mail.imap.socketFactory.port", "993");
        props.put("mail.imap.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        props.put("mail.imap.socketFactory.fallback", "false");
        
        System.out.print("[beta] Please enter the password for the AdminTracker account: ");
        Scanner scanner = new Scanner(System.in);
        pass = scanner.nextLine();
        scanner.close();
        
        System.out.println("Opening email session...");
        Session session = Session.getInstance(props, new javax.mail.Authenticator() 
        {
            protected PasswordAuthentication getPasswordAuthentication()
            { 
                return new PasswordAuthentication(user, pass);    
            }
        });
        
        try
        {
            Store store = session.getStore("imap");
            store.connect(host, user, pass);
            System.out.print("Connecting...");
            if(store.isConnected())
            {
                System.out.println("Complete.\nGetting messages...");
                Folder folder = store.getFolder("INBOX");
                folder.open(Folder.READ_ONLY);
                System.out.println("Messages received successfully.");
                return folder.getMessages();
            }
            else
            {   
                System.out.println("Failed.");
            }
        }
        catch (Exception e)
        {
            System.out.println("Unable to access AdminTracker inbox.");
            e.printStackTrace();
        }
        
        return null;
    }

    private static String host = "smtp.gmail.com";
    
    public static void sendMessage(String recipient, String subject, String content) throws Exception
    {
        Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
         
        Properties props = new Properties();
        
        props.setProperty("mail.transport.protocol", "smtp");
        props.setProperty("mail.host", host);
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.port", "465");
        props.put("mail.debug", "false");
        props.put("mail.smtp.socketFactory.port", "465");
        props.put("mail.smtp.socketFactory.class",
        "javax.net.ssl.SSLSocketFactory");
        props.put("mail.smtp.socketFactory.fallback", "false");
        props.setProperty("mail.smtp.quitwait", "false");
 
        Session session = Session.getInstance(props, new javax.mail.Authenticator() 
        {
            protected PasswordAuthentication getPasswordAuthentication()
            { return new PasswordAuthentication(user, pass);    }
        });
        
            MimeMessage message = new MimeMessage(session);
            message.setSender(new InternetAddress(user, "AdminTracker"));
            message.setSubject(subject);
            message.setContent(content +            "\n\nSincerely,\nYour Troy NJROTC BRO (Battalion Roster Officer)\nDepartment of Administration", "text/plain");
            message.setRecipient(Message.RecipientType.TO, new InternetAddress(recipient));
            
            SMTPTransport smtpt = new SMTPTransport(session, new URLName(host));
            smtpt.send(message);
    }
}